<?php require 'head.php' ?>
<?php
$display_info = false;
$act = '';
if (isset($_GET['act'])) {
    $act = $_GET['act'];
    // 删除
    if(isset($_GET['act']) == 'del' && isset($_GET['key'])
       && isset($kw_categories[$_GET['key']])){
        unset($kw_categories[$_GET['key']]);
        
        $code = "<?php\n\$kw_categories = ".var_export($kw_categories, true)."\n?>";
        file_put_contents(KW_DATA.'categories/index.php', $code);
    
        //重定向浏览器
        Header("Location:cateMng.php?done=".$act);
        //确保重定向后，后续代码不会被执行
        exit();
    }
}

if (isset($_POST['save'])) {
    $post_cate_key_init = $_POST['cate_key_init'];
    $post_cate_key = $_POST['cate_key'];
    $post_cate_name = $_POST['cate_name'];
    $post_cate_class = $_POST['cate_class'];
    $post_cate_order = $_POST['cate_order'];
    // ADD
    if($post_cate_key_init == ''){
        $act = 'add';
        $kw_categories[$post_cate_key] = array();
        // UPD
    } else {
        $act = 'upd';
        // key值（分类别名）没变。
        if($post_cate_key_init == $post_cate_key) {
        // key值（分类别名）变了。
        } else {
            // TODO 检查是否有重复存在
            
            
            unset($kw_categories[$post_cate_key_init]);
            $kw_categories[$post_cate_key] = array();
        }
    }
    $kw_categories[$post_cate_key]['name'] = $post_cate_name;
    $kw_categories[$post_cate_key]['class'] = $post_cate_class;
    $kw_categories[$post_cate_key]['order'] = $post_cate_order;

    // 根据order进行数组排序
    foreach($kw_categories as $value)
        $t[] = $value['order'];
 
    array_multisort($t, SORT_ASC, $kw_categories); //排序
    //print_r(array_slice($kw_categories, 0, 3)); //取出前3条

    $code = "<?php\n\$kw_categories = ".var_export($kw_categories, true)."\n?>";
    file_put_contents(KW_DATA.'categories/index.php', $code);

    //重定向浏览器
    Header("Location:cateMng.php?done=".$act);
    //确保重定向后，后续代码不会被执行
    exit();
}

$cate_key = '';
$cate_name = '';
$cate_class = '';
$cate_order = '';
if(isset($_GET['key']) && isset($kw_categories[$_GET['key']])){
    $cate_key = $_GET['key'];
    $cate_name = $kw_categories[$key]['name'];
    $cate_class = $kw_categories[$key]['class'];
    $cate_order = $kw_categories[$key]['order'];
}

?>

<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
  <input type="hidden" name="cate_key_init" value="<?php echo htmlspecialchars($cate_key); ?>" />
  <?php if ($display_info) { ?>
  <div class="updated">保存成功！</div>
  <?php } ?>
  <div class="admin_page_name">
    <?php if ($cate_key != ''){ ?>修改<?php } else { ?>添加<?php } ?>分类
  </div>
  <div class="small_form small_form2">
    <div class="field">
      <div class="label">分类名称</div>
      <input class="textbox" type="text" name="cate_name" value="<?php echo htmlspecialchars($cate_name); ?>" />
      <div class="info">中文名。</div>
    </div>
    <div class="clear"></div>
    <div class="field">
      <div class="label">分类别名</div>
      <input class="textbox" type="text" name="cate_key" value="<?php echo htmlspecialchars($cate_key); ?>" />
      <div class="info">英文名。</div>
    </div>
    <div class="clear"></div>
    <div class="field">
      <div class="label">分类类别</div>
      <input name="cate_class" type="radio" value="0" <?php if($cate_class != '1'){ echo 'checked';} ?> />单页
      <input name="cate_class" type="radio" value="1" <?php if($cate_class == '1'){ echo 'checked';} ?> />列表
    </div>
    <div class="clear"></div>
    <div class="field">
      <div class="label">排序</div>
      <input class="textbox" type="text" name="cate_order" value="<?php echo htmlspecialchars($cate_order); ?>" />
      <div class="info">最好只输入数字。</div>
    </div>
    <div class="clear"></div>
    <div class="field">
      <div class="label"></div>
      <div class="field_body"><input class="button" type="submit" name="save" value="保存数据" /></div>
      <div class="info"></div>
    </div>
    <div class="clear"></div>
  </div>
</form>
<?php require 'foot.php' ?>